package ms.platform.progress.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import ms.platform.progress.dao.FactoryPlanDao;
import ms.platform.progress.dao.IndexAuditDao;
import ms.platform.progress.dto.IndexAuditDto;
import ms.platform.progress.dto.ProgressDto;
import ms.platform.progress.model.AuditResult;
import ms.platform.util.PageUtils;

@Service
@Transactional(propagation=Propagation.SUPPORTS)
public class IndexAuditService {
	
	@Autowired
	IndexAuditDao indexAuditDao;
	
	@Autowired
	FactoryPlanDao factoryPlanDao;
	/**
	 * 首页列表
	 * @return
	 */
	public List<IndexAuditDto> queryPageAuditList(Map<String, Object> map,HttpServletRequest request,String userId){
		List<String> roleId = indexAuditDao.queryRoleIdByUserId(userId);
		if(null != roleId){
			for (String string : roleId) {
				if(string.equals("17")){
					map.put("reviewStatus", "1");
				}
			}
		}
		map.put("count", indexAuditDao.queryPageAuditCount(map));
		List<IndexAuditDto> list = indexAuditDao.queryPageAuditList(PageUtils.Page(request,map));
		if("1".equals(map.get("reviewStatus"))){
			for (IndexAuditDto indexAuditDto : list) {
				indexAuditDto.setFlag("1");
			}
		}
		PageUtils.calculate(map);
		return list;
	}

	public IndexAuditDto queryAudit(Integer recordId){
		 IndexAuditDto indexAuditDto = indexAuditDao.queryAudit(recordId);
		return indexAuditDto;
	}
	
	public void audit(Map<String, Object> map,AuditResult auditResult){
		if("1".equals(map.get("status"))){
			map.put("reviewStatus", "1");
		}else{
			map.put("reviewStatus", "0");
		}
		indexAuditDao.updateAuditStatus(map);
		String userId = (String) map.get("userId");
		if(userId!=null){
			String roleName = indexAuditDao.queryRoleNameByUserId(userId);
			auditResult.setRole(roleName);
			//auditResult.setRevirwUser(Integer.parseInt(userId));
			auditResult.setInsertedBy(userId);
			auditResult.setInsertedAt(new Date());
			auditResult.setUpdatedBy(userId);
			auditResult.setUpdatedAt(new Date());
		}
		indexAuditDao.saveAuditResult(auditResult);
		if(null != auditResult.getId()){
			ProgressDto progressDto = factoryPlanDao.getProgressRecordById(auditResult.getRecordId());
			Map<String,Object> detailMap = new HashMap<String,Object>();
			detailMap.put("id", progressDto.getRecordDetailId());
			detailMap.put("reviewRcdId", auditResult.getId());
			indexAuditDao.associatedAuditRecordId(detailMap);
		}
		/*if(auditResult.getAccPro().equals("100")){
			//更新工序结束时间
			Map<String,Object> progressPlanMap = new HashMap<String,Object>();
			map.put("id", auditResult.getModelProgressId());
			map.put("accEndTime", new Date());
			factoryPlanDao.updateModelProgressPlanTime(progressPlanMap);
		}*/
		
	}
	
	public void reviewAudit(Map<String, Object> map,AuditResult auditResult){
		if("1".equals(map.get("status"))){
			map.put("status", "2");
			map.put("reviewStatus","2");
			indexAuditDao.updateProgressStatus(auditResult);
		}else{
			map.put("status",'3');
			map.put("reviewStatus","0");
		}
		indexAuditDao.updateAuditStatus(map);
		String userId = (String) map.get("userId");
		if(userId!=null){
			//String roleName = indexAuditDao.queryRoleNameByUserId(userId);
			//auditResult.setRole(roleName);
			auditResult.setReviewAdvise(auditResult.getAdvise());
			auditResult.setRevirwUser(Integer.parseInt(userId));
			//auditResult.setReviewTime(new Date());
			auditResult.setUpdatedBy(userId);
			auditResult.setUpdatedAt(new Date());
		}
		indexAuditDao.updateAuditResult(auditResult);
		/*if(null != auditResult.getId()){
			Map<String,Object> detailMap = new HashMap<String,Object>();
			detailMap.put("recordId", auditResult.getRecordId());
			detailMap.put("reviewRcdId", auditResult.getId());
			indexAuditDao.associatedAuditRecordId(detailMap);
		}*/
		if(auditResult.getAccPro().equals("100")){
			//更新工序结束时间
			Map<String,Object> progressPlanMap = new HashMap<String,Object>();
			progressPlanMap.put("id", auditResult.getModelProgressId());
			progressPlanMap.put("accEndTime", new Date());
			factoryPlanDao.updateModelProgressPlanTime(progressPlanMap);
		}
		
	}
	
	public String queryRoleIdByUserId(String userId){
		List<String> roleId = indexAuditDao.queryRoleIdByUserId(userId);
		if(null != roleId){
			for (String string : roleId) {
				if(string.equals("17")){
					return "true";
				}
			}
		}
		return "false";
	}
	
	/**
	 * 根据计划id和当前工序名称查询计划工序ID
	 * @param map
	 * @return
	 */
	public Integer queryProcessIdByPlanIdAndPlanName(Map<String, String> map){
		return indexAuditDao.queryProcessIdByPlanIdAndPlanName(map);
	}
}
